/*
See LICENSE folder for this sample’s licensing information.

Abstract:
Implements the collection view cell for displaying an asset in the grid view.
*/

import UIKit

class GridViewCell: UICollectionViewCell {
    
    @IBOutlet var imageView: UIImageView!
    @IBOutlet var livePhotoBadgeImageView: UIImageView!
    
    var representedAssetIdentifier: String!
    let selectedIcon = UIImageView()
    

    
    var thumbnailImage: UIImage! {
        didSet {
            imageView.image = thumbnailImage
        }
    }
    var livePhotoBadgeImage: UIImage! {
        didSet {
            livePhotoBadgeImageView.image = livePhotoBadgeImage
        }
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        imageView.image = nil
        livePhotoBadgeImageView.image = nil
        selectedIcon.isHidden = true
    }
    
    required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            // 配置 selectedIcon
            selectedIcon.image = UIImage(systemName: "checkmark.circle.fill") // 使用你的选中图标
            selectedIcon.contentMode = .center
            selectedIcon.clipsToBounds = true
            selectedIcon.isHidden = true
            addSubview(selectedIcon)
            selectedIcon.translatesAutoresizingMaskIntoConstraints = false
            NSLayoutConstraint.activate([
                selectedIcon.widthAnchor.constraint(equalToConstant: 30),
                selectedIcon.heightAnchor.constraint(equalToConstant: 30),
                selectedIcon.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -10),
                selectedIcon.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -10)
            ])

        }
    

    override var isSelected: Bool {
            didSet {
                imageView.alpha = isSelected ? 0.5 : 1.0 // 修改透明度
                selectedIcon.isHidden = !isSelected // 显示或隐藏选中图标
            }
    }
    
}
